﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebSite.Repositories;
using WebSite.Services;

namespace WebSite.HttpModules
{
    public class AuthenticationHttpModule:IHttpModule
    {
        public AuthenticationHttpModule(){
        }

        public void Init(HttpApplication context)
        {
            context.AuthenticateRequest += context_AuthenticateRequest;
        }

        private void context_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;

            //TODO: ADD HONEST DI HERE!!!
            IUserRepository userRepo = new UserRepository();
            IHttpCookiesService cookiesService = new HttpCookiesService(HttpContext.Current);
            IAuthenticationService authService = new AuthenticationService(cookiesService, userRepo);
            
            context.User = authService.CurrentUser;
        }

        public void Dispose()
        {
            //throw new NotImplementedException();
        }
    }
}